import { Injectable, HttpStatus, HttpException } from '@nestjs/common';
import { InjectRepository, } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Friendships } from '../../entities/friendships.entity'
import { CreateFriendshipsDto } from './dtos/create-friendships.dto'
import { UpdateFriendshipsDto } from './dtos/update-friendships.dto'
@Injectable()
export class FriendshipsService {
    constructor(
        @InjectRepository(Friendships)
        private readonly fsriendshipsRepository: Repository<Friendships>
    ) { }

    async createFriendship(createFriendshipsDto: CreateFriendshipsDto, user_id: string) {
        // 先判断是否是好友关系
        // const res = await this.fsriendshipsRepository.createQueryBuilder('friendships')

        //     .leftJoinAndSelect('friendships.user', 'user')
        //     .leftJoinAndSelect('friendships.friend', 'friend')

        //     .getMany()

        // const res = await this.fsriendshipsRepository.find({
        //     where: [
        //         // {
        //         //     user:,
        //         //     friend: createFriendshipsDto.friend_id
        //         // }
        //     ],
        //     relations: ['user', 'friend']
        // })

        // if (res) {
        //     return "已是好友关系"
        // }

        // return res

        await this.fsriendshipsRepository.save({ ...createFriendshipsDto, user_id })
        throw new HttpException('已发送好友申请', HttpStatus.OK)
    }

    async updateFriendship(updateFriendshipsDto: UpdateFriendshipsDto, user_id: string) {
        // if(createFriendshipsDto.status === 1)
        // 查询当前数据
        const fsriendships = await this.fsriendshipsRepository.findOne({
            where: { friendships_id: updateFriendshipsDto.friendships_id }
        })
        // 查不到数据
        if (!fsriendships) throw new HttpException('friendships_id传入错误', HttpStatus.BAD_REQUEST)

        // 验证是否是好友
        // if (fsriendships.friend.id === user_id || fsriendships.user.id === user_id) {
        //     throw new HttpException('传入id不是好友的id', HttpStatus.BAD_REQUEST)
        // }

        if (updateFriendshipsDto.status !== undefined && updateFriendshipsDto.block_status != undefined) {
            throw new HttpException('status和block_status不能同时存在', HttpStatus.BAD_REQUEST)
        }

        // 待审核
        if (updateFriendshipsDto.status !== undefined) {
            // 加好友或者重新添加好友
            if (fsriendships.status == 0 || fsriendships.status == 2) {
                await this.fsriendshipsRepository.save({
                    ...fsriendships,
                    ...updateFriendshipsDto
                })
                // 同意添加好友
                if (updateFriendshipsDto.status == 1) return new HttpException('已同意添加好友', HttpStatus.OK)

                // 拒绝添加好友
                if (updateFriendshipsDto.status == 2) return new HttpException('已拒绝添加好友', HttpStatus.OK)
            } else {
                return new HttpException('您俩已是好友关系', HttpStatus.OK)
            }
        }

        // 拉黑
        if (updateFriendshipsDto.block_status !== undefined) {
            if (fsriendships.status == 1) {
                await this.fsriendshipsRepository.save({
                    ...fsriendships,
                    ...updateFriendshipsDto
                })
                if (updateFriendshipsDto.block_status == 1) return new HttpException('已拉黑好友', HttpStatus.OK)
                if (updateFriendshipsDto.block_status == 0) return new HttpException('已取消拉黑好友', HttpStatus.OK)
            } else {
                throw new HttpException('删除失败,您俩不是好友关系', HttpStatus.BAD_REQUEST)
            }
        }
    }

    async deleteFriendship(friend_id: string, user_id: string) {
        const friendship = await this.fsriendshipsRepository
            .createQueryBuilder('friendships')
            .leftJoinAndSelect('friendships.user', 'friendshipsUser')
            .leftJoinAndSelect('friendships.friend', 'friendshipsFriend')
            .where('(friendshipsUser.id = :user_id AND friendshipsFriend.id = :friend_id) OR (friendshipsUser.id = :friend_id AND friendshipsFriend.id = :user_id)',
                { user_id, friend_id })
            .getOne()

        console.log(friendship)
        if (!friendship) throw new HttpException('删除失败,您俩不是好友关系', HttpStatus.BAD_REQUEST)

        if (friendship.status === 1) {
            // 将他们的关系变为3
            await this.fsriendshipsRepository.save({
                ...friendship,
                status: 3
            })

            throw new HttpException('删除成功', HttpStatus.OK)
        } else {
            throw new HttpException('删除失败,您俩不是好友关系', HttpStatus.BAD_REQUEST)
        }



    }
}
